<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Default page for starting or managing shows.
 *  This same page starts or edits a show, depending on whether a show id (sid)
 *  has been provided, or not. In all cases, it is assumed that the viewer
 *  has modification permisions for the show. Otherwise, default_member should
 *  be used instead.
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 
// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access. Sorry');
 
// Add the stylesheet for this page
JHTML::_('stylesheet','base.css','components/com_musashi/views/common/');
JHTML::_('script','jquery-1.5.min.js','components/com_musashi/views/common/');
JHTML::_('stylesheet','default.css','components/com_musashi/views/shows/tmpl/');

// Get the show id, if any
$showID = JRequest::getInt('sid',-1);

// We'll be needing this model
$model =& $this->getModel();


/**
 *  Checks to see if there is a message from a previous action. Clears the flag if necessary
 */
function getActionResult( $actionFlag )
{
    $session =& JFactory::getSession();
    
    // Get previous message, then clear
    $msg = $session->get($actionFlag,'');
    $session->set($actionFlag,'');
    
    return $msg;
}


?>
<script language="javascript" type="text/javascript">
<!--

// Compatibility mode for jQuery
var _jq = jQuery.noConflict();

// Makes sure the provided date is on the yyyy-mm-dd format
function isValidDate( theDate )
{
    // 1. Check for format
    var dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;
    if( !dateFormat.test(theDate) )
    {
        return false;
    }
    
    // 2. Check for ranges
    var components = theDate.split('-');
    var dateObj    = new Date( components[0], components[1] - 1, components[2] );
        
    if( (dateObj.getFullYear() != components[0]) || (dateObj.getMonth() + 1 != components[1]) || (dateObj.getDate() != components[2]) )
    {
        return false;
    }
    
    return true;
    
    
}

// Validates the base form
function validateBaseForm()
{
    var theForm = document.baseForm;
    
    // All entries must exist
    if( (theForm.name) && (theForm.name.value == "") )
    {
        alert("Please indicate a show name");
        return false;
    }
    
    if( theForm.date.value == "" )
    {
        alert("Please indicate a show date");
        return false;
    }
    
    // Check the date's format
    if(!isValidDate(theForm.date.value))
    {
        alert("Please correct the specified date.\nFormat should be: yyyy-mm-dd");
        return false;
    }
    
    // Configure date and time
    var hour = parseInt(theForm.baseHour.options[theForm.baseHour.selectedIndex].text);
    var min  = parseInt(theForm.baseMinute.options[theForm.baseMinute.selectedIndex].text);
    var date = theForm.date.value;
    
    if( theForm.basePart.options[theForm.basePart.selectedIndex].text == "pm" ){
        hour += 12;
    }
    
    var dateTime = date + ' ' + hour + ':' + min + ':00';
    theForm.datetime.value = dateTime;
    
    return true;    
}


-->
</script>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->


<musashiContent id='content' class='body'>
    
    <header id="title" class="body">
        <div class="name">
            <h1><?php if($showID == -1) { echo 'Start a show'; } else { echo 'Configuring your show'; }?></h1>
        </div>
        <div class="description">
        <p>This page allows you to start a new show. Include
        all information available at the moment. You can add or edit any
        details later</p>
        </div>
    </header>
    
    <?php
    
    // -------------------------------------------------------------------------
    // Previous action result
    // -------------------------------------------------------------------------
    $previousMessage = getActionResult('errorMessage');
    if( $previousMessage != '')
    {?>
        <div class="error"><?php echo $previousMessage; ?></div>
    <?php        
    }
    ?>

    <section id='base-panel' class="body">
        <h2>Basic Information</h2>
        <form id='form-base' name='baseForm' method='post' onsubmit='return validateBaseForm()' action="<?php echo JRoute::_('index.php?');?>">
          <fieldset>
            <ol>
                <li>
                    <label for="name">Show name</label>
                    <input type="text" id="name" name='name' maxlength="45"/>
                </li>                
                <li>
                    <label for="date">Date (yyyy-mm-dd)</label>
                    <?php echo JHTML::_('calendar', $date,'date', 'date', '%Y-%m-%d'); ?>                    
                </li>
                <li>
                    <label for="time">Time</label>
                    <select name='select-hour' id='baseHour' style="margin-left:10px;">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option selected>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>                        
                    </select>
                    :
                    <select name='select-minute' id='baseMinute' >
                        <option selected>00</option>
                        <option>10</option>
                        <option>20</option>
                        <option>30</option>
                        <option>40</option>
                        <option>50</option>
                    </select>
                    /
                    <select name='select-part' id='basePart'>
                        <option>am</option>
                        <option selected>pm</option>
                    </select>                    
                </li>
                
            </ol>
            <input type="submit" value="Start show" class="base-submit" />
          </fieldset>
          <input type="hidden" name="option"     value="com_musashi" />
          <input type="hidden" name="task"       value="newShow" />
          <input type="hidden" name="controller" value="shows" />
          <input type="hidden" name="datetime"   value="" />
        </form>
    </section>    
    
    <!-- Footer. Display syndication, and some copyright information -->
    <footer id="footer" class="body">
        <div class="footer-data">
            <h2>The Musashi Project</h2>
            <h3>A Framework for Managing Your Group</h3>
            <p>(c)2010 - The Musashi Project</p>                    
        </div>
    </footer>
    
    
</musashiContent>










